<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Console\Application;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

use Log;
use App\ConvertTask;
use App\Services\ConvertService;

class ConvertAwardFormDataToPdfJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */

    public $tries = 3;

    protected $task;

    public function __construct(ConvertTask $task)
    {
        $this->task = $task;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $task = $this->task;
        Log::info("文档生成开始, 任务id: {$task->award_id}");
        $convertService = new ConvertService();
        $barcode = date("YmdHis") . '-' . $task->award_id;
        $uuid = $convertService->uuid();
        $resultPath = '/awardConvertDocs/'. date('Ymd') . '/' . $uuid;
        $error = $convertService->convertAwardFormDataToWordDocument($task->id, $task->json_data, $task->award_id, $task->award_type, $resultPath, $barcode);
        if (empty($error)) {
            Log::info("文档生成任务完成");
        } else {
            Log::info("文档生成任务失败");
        }
    }
}
